//
//  MyCollectVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/1.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyCollectVC.h"
#import "SubjectTabVC.h"
#import "MyCollectDemandTabVC.h"
#import "MyCollectServiceTabVC.h"
#import "MyCollectShopTabVC.h"
#import "SubjectRecommendContentTabVC.h"
#import "MyCollectShopContentVC.h"
#import "DemandTheHallContentVC.h"
#import "ServiceContentTabVC.h"
#import "HomeModel.h"
#import "ShopModel.h"
#import "ShopSubModel.h"
@interface MyCollectVC ()<UIScrollViewDelegate>
@property(nonatomic,strong) MCButton *mcBtn;
@property(nonatomic,strong) MCButton *preMCBtn;
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) SubjectTabVC *subjectVC;
@property(nonatomic,strong) MyCollectDemandTabVC *demandVC;
@property(nonatomic,strong) MyCollectServiceTabVC *serviceVC;
@property(nonatomic,strong) MyCollectShopTabVC *indicatorTabVC;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary  *dict;
@end

@implementation MyCollectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.manager = [AFHTTPSessionManager manager];
    CGFloat w = kVWidth/4;
    NSArray *titles = @[@"专题",@"需求",@"服务",@"店铺"];
    for (int i = 0; i < 4; i++) {
        self.mcBtn = [[MCButton alloc]initWithFrame:CGRectMake(w*i, 0, w, 40)];
        [self.view addSubview:self.mcBtn];
        [self.mcBtn setTitle:titles[i] forState:UIControlStateNormal];
        self.mcBtn.tag = i;
        if (i == 0) {
            [self.mcBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            [self.mcBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
        }else{
            [self.mcBtn setImage:[UIImage new] forState:UIControlStateNormal];
            [self.mcBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
        }
        self.mcBtn.buttonStyle = imageNormalBottom;
        self.mcBtn.titleLabel.font = [UIFont systemFontOfSize:14];
         [self.mcBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        self.mcBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.mcBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
    }
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.mcBtn.frame), kVWidth, kVHeight-self.mcBtn.frame.size.height-kNavAndStatusHight)];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(titles.count*kUIScreen.size.width, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.backgroundColor = kWhite;
    [self.view addSubview:self.scrollView];
    self.subjectVC = [[SubjectTabVC alloc]init];
    self.subjectVC.view.frame = CGRectMake(0, 10, kUIScreen.size.width, self.scrollView.frame.size.height-10);
    __weak typeof (self)weakSelf = self;
    self.subjectVC.returnValueBlock = ^(NSInteger index, SpecialRecommendationModel * _Nonnull subjectModel) {
        SubjectRecommendContentTabVC *homeVC = [[SubjectRecommendContentTabVC alloc]init];
        homeVC.specialModel =subjectModel;
        homeVC.returnValueBlock = ^(NSInteger selected) {
            [weakSelf.subjectVC reloadInternetData];
        };
        homeVC.index = index;
        [weakSelf.navigationController pushViewController:homeVC animated:YES];
    };
    [self.scrollView addSubview:self.subjectVC.view];
    self.demandVC = [[MyCollectDemandTabVC alloc]init];
    self.demandVC.view.frame = CGRectMake(kVWidth, 10, kUIScreen.size.width, self.scrollView.frame.size.height-10);
    self.demandVC.returnValueBlock = ^(NSInteger row, HomeModel * _Nonnull homeModel) {
        DemandTheHallContentVC *vc = [[DemandTheHallContentVC alloc]init];
//        vc.returnValueBlock = ^(NSInteger selected) {
////                [weakSelf.demandVC refresh];
//        };
        
        vc.homeModel = homeModel;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    [self.scrollView addSubview:self.demandVC.view];
    self.serviceVC = [[MyCollectServiceTabVC alloc]init];
    self.serviceVC.view.frame = CGRectMake(kVWidth*2, 10, kUIScreen.size.width, self.scrollView.frame.size.height-10);
    self.serviceVC.returnValueBlock = ^(NSInteger selected, ServiceModel * _Nonnull takeModel) {
        ServiceContentTabVC *homeServiceVC = [[ServiceContentTabVC alloc]init];
        homeServiceVC.returnValueBlock = ^(NSInteger selected) {
            [weakSelf.serviceVC refresh];
        };
        homeServiceVC.model = takeModel;
        [weakSelf.navigationController pushViewController:homeServiceVC animated:YES];
    };
    [self.scrollView addSubview:self.serviceVC.view];
    self.indicatorTabVC = [[MyCollectShopTabVC alloc]init];
    self.indicatorTabVC.view.frame = CGRectMake(kVWidth*3, 0, kUIScreen.size.width, self.scrollView.frame.size.height-10);
    self.indicatorTabVC.returnValueBlock = ^(ShopSubModel * shopModel) {
        MyCollectShopContentVC *vc = [[MyCollectShopContentVC alloc]init];
        
        vc.model = shopModel;
        vc.title = shopModel.title;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    };
    [self.scrollView addSubview:self.indicatorTabVC.view];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/kVWidth;
    for (id obj in self.view.subviews) {
        
        if ([obj isKindOfClass:[MCButton class]]) {
            MCButton *btn = (MCButton *)obj;
            if (btn.tag == index) {
                XLog(@"btn:%@",btn);
                [btn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
                [btn setTitleColor:kLabel16Color forState:UIControlStateNormal];
            }else{
                [btn setImage:[UIImage new] forState:UIControlStateNormal];
                [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
            }
        }
        
    }
}
-(void)clickBtn:(MCButton *)clickBtn
{
    [self.preMCBtn setImage:[UIImage new] forState:UIControlStateNormal];
    [self.preMCBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
    self.scrollView.contentOffset = CGPointMake(clickBtn.tag*kVWidth, 0);
    [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
    [clickBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
    self.preMCBtn = clickBtn;
    
}
@end
